package com.spirngboot.middleware.middleware_rabbitmq.server.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadOrderRabbitMQConfig {
    //死信交换机
    @Bean
    public DirectExchange deadOrderExchange() {
        return new DirectExchange("dead-direct-exchange", false, false);
    }

    //死信队列
    @Bean
    public Queue deadOrderQueue() {
        return new Queue("dead_order.direct.queue", false, false, true);
    }

    //绑定死信交换机和死信队列
    @Bean
    public Binding deadOrderQueueBinding() {
        return BindingBuilder.bind(deadOrderQueue()).to(deadOrderExchange()).with("dead_order");
    }

    //设置TTL的交换机
    @Bean
    public DirectExchange orderTTLExchange() {
        return new DirectExchange("orderTTL-exchange", false, false);
    }

    //设置TTL的队列,同时设置其绑定的死信队列
    @Bean
    public Queue orderTTLQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 5000);        //这里设置了一个有过期时间的队列,过期时间5s
        args.put("x-dead-letter-exchange", "dead-direct-exchange"); //绑定死信队列的交换机
        args.put("x-dead-letter-routing-key", "dead_order"); //死信队列的RoutingKey,如果死信队列是fanout则没有
        return new Queue("orderTTL.direct.queue", false, false, false, args);
    }

    //绑定orderTTL队列和交换机
    @Bean
    public Binding orderTTLQueueBindingAndDeadQueue() {
        return BindingBuilder.bind(orderTTLQueue()).to(orderTTLExchange()).with("");
    }
}
